CXX := g++
CXXFLAGS := -std=c++17 -Wall -Wextra -I./include
LDFLAGS := -lmysqlclient -ljsoncpp -lpthread

# 自动查找所有cpp文件
SRCS := $(wildcard src/*.cpp)
OBJS := $(SRCS:.cpp=.o)
TARGET := bin/server

# 调试模式配置
DEBUG ?= 1
ifeq ($(DEBUG), 1)
    CXXFLAGS += -g -O0 -DDEBUG
else
    CXXFLAGS += -O3
endif

.PHONY: all clean run

all: $(TARGET)

$(TARGET): $(OBJS)
	@mkdir -p $(@D)
	$(CXX) $^ -o $@ $(LDFLAGS)

%.o: %.cpp
	$(CXX) $(CXXFLAGS) -c $< -o $@

clean:
	rm -rf $(OBJS) $(TARGET) bin/

run: $(TARGET)
	./$(TARGET)

# 静态代码检查
analyze:
	cppcheck --enable=all --suppress=missingIncludeSystem -Iinclude src/

# 内存检查
memcheck: $(TARGET)
	valgrind --leak-check=full ./$(TARGET)